package com.swang.pms.service.common.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.swang.pms.dal.common.SysDictionaryDao;
import com.swang.pms.model.common.GridUtil;
import com.swang.pms.model.common.Pager;
import com.swang.pms.model.dto.common.SysDictionaryDto;
import com.swang.pms.model.enums.DictionaryGroup;
import com.swang.pms.model.enums.Status;
import com.swang.pms.service.common.SysDictionaryService;

/**
 * @ClassName: SysDictionaryServiceImpl
 * @Description: 数据字典
 * @author 王泽
 * @date 2017年3月16日 下午9:45:51
 */
@Service("sysDictionaryService")
public class SysDictionaryServiceImpl implements SysDictionaryService {

    @Resource
    SysDictionaryDao sysDictionaryDao;

    public Pager pageFindSysDictionaryGrid(Pager page, SysDictionaryDto sysDictionaryDto) {
        String[] paramSerial = new String[] { "id", "group_name", "group_code", "dict_name", "dict_code", "ext_value", "is_cache",
                "sys_flag", "gmt_create", "gmt_modify", "status", "sort_num", "operator", "remark" };
        sysDictionaryDto.setStartIndex(page.getRowStartIndex());
        sysDictionaryDto.setPageSize(page.getPageSize());
        List<Map<String, Object>> list = this.sysDictionaryDao.pageFindSysDictByDto(sysDictionaryDto);
        sysDictionaryDto.setStartIndex(null);
        sysDictionaryDto.setPageSize(null);
        page.setRowCount(this.sysDictionaryDao.pageFindSysDictCountByDto(sysDictionaryDto));
        page.setGridr(GridUtil.getGridList(list, paramSerial, page.getRowStartIndex(), page.getPageSize(), page.getRowCount(), true));
        return page;
    }

    public Pager findAllSysDictGroupGrid(Pager page, SysDictionaryDto sysDictionaryDto) {
        String[] paramSerial = new String[] { "id", "group_code", "group_name", "remark" };
        sysDictionaryDto.setStartIndex(null);
        sysDictionaryDto.setPageSize(null);
        sysDictionaryDto.setGroupCode(DictionaryGroup.DICTIONARY_GROUP.getCode());
        List<Map<String, Object>> list = this.sysDictionaryDao.findAllSysDictGroup(sysDictionaryDto);
        page.setRowCount(org.springframework.util.CollectionUtils.isEmpty(list) ? 0 : list.size());
        page.setGridr(GridUtil.getGridList(list, paramSerial, true));
        return page;
    }

    public List<SysDictionaryDto> getSysDictionaryList(SysDictionaryDto sysDictionaryDto) {
        return this.sysDictionaryDao.listSysDict(sysDictionaryDto);
    }

    public Boolean deleteByPrimaryKey(SysDictionaryDto sysDictionaryDto) {
        sysDictionaryDto.setStatus(Status.DELETE.getCode());
        return this.sysDictionaryDao.updateByPrimaryKey(sysDictionaryDto) == 1;
    }

    public Boolean insertSysDict(SysDictionaryDto sysDictionaryDto) {
        return this.sysDictionaryDao.insertSysDict(sysDictionaryDto) == 1;
    }

    public Boolean updateByPrimaryKey(SysDictionaryDto sysDictionaryDto) {
        return this.sysDictionaryDao.updateByPrimaryKey(sysDictionaryDto) == 1;
    }

    public int countOneSysDict(SysDictionaryDto sysDictionaryDto) {
        return this.sysDictionaryDao.countOneSysDict(sysDictionaryDto);
    }

    public List<SysDictionaryDto> getAllEnums() {
        return this.sysDictionaryDao.getAllEnums();
    }

    public List<SysDictionaryDto> getProvince(SysDictionaryDto sysDictionaryDto) {
        return this.sysDictionaryDao.getProvince(sysDictionaryDto);
    }

    public List<SysDictionaryDto> getCity(SysDictionaryDto sysDictionaryDto) {
        return this.sysDictionaryDao.getCity(sysDictionaryDto);
    }

    public List<SysDictionaryDto> getArea(SysDictionaryDto sysDictionaryDto) {
        return this.sysDictionaryDao.getArea(sysDictionaryDto);
    }

    public Boolean updateDictGroup(SysDictionaryDto sysDictionaryDto) {
        try {
            this.sysDictionaryDao.updateByPrimaryKey(sysDictionaryDto);
            this.sysDictionaryDao.updateGroupNameByGroupCode(sysDictionaryDto);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public SysDictionaryDto getDictionary(SysDictionaryDto sysDictionaryDto) {
        return this.sysDictionaryDao.getDictionary(sysDictionaryDto);
    }

    public Pager pageFindSysDictGridByDto(Pager page, SysDictionaryDto sysDictionaryDto) {
        String[] paramSerial = new String[] { "id", "group_code", "group_name", "dict_code", "dict_name", "is_cache", "sys_flag",
                "sort_num", "remark" };
        List<Map<String, Object>> list = this.sysDictionaryDao.pageFindSysDictByDto(sysDictionaryDto);
        page.setRowCount(org.springframework.util.CollectionUtils.isEmpty(list) ? 0 : list.size());
        page.setGridr(GridUtil.getGridList(list, paramSerial, true));
        return page;
    }

    public SysDictionaryDto getProvinceByCity(SysDictionaryDto sysDictionaryDto) {
        sysDictionaryDto.setDictCode(sysDictionaryDto.getDictCode().substring(0, 2) + "0000");
        sysDictionaryDto.setGroupCode(DictionaryGroup.AREA_PROVINCE.getCode());
        sysDictionaryDto.setStatus(Status.PUBLISH.getCode());
        return sysDictionaryDao.getProvinceByCity(sysDictionaryDto);
    }
}
